
//
//  ZFBHomeBottomView.m
//  01-ZFB
//
//  Created by My Mac on 2017/6/3.
//  Copyright © 2017年 MyMac. All rights reserved.
//

#import "ZFBHomeBottomView.h"
#import "ZFBHomeBottomListFlowLayout.h"
#import "ZFBHomeBottomListCell.h"
#import "ZFBHomeBottomList.h"
static NSString *cellID = @"listCell";

@interface ZFBHomeBottomView()<UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation ZFBHomeBottomView

-(void)awakeFromNib
{
    [super awakeFromNib];
    [self setUpUI];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpUI];
    }
    return self;
}

-(void)setUpUI
{
    ZFBHomeBottomListFlowLayout *flowFayout = [[ZFBHomeBottomListFlowLayout alloc]init];
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowFayout];
    //设置代理
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self addSubview:collectionView];
    
    [collectionView registerClass:[ZFBHomeBottomListCell class] forCellWithReuseIdentifier:cellID];
    collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.offset(0);
    }];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _bottomDataArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZFBHomeBottomListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.bottomList = _bottomDataArr[indexPath.item];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZFBHomeBottomListCell *cell = (ZFBHomeBottomListCell *)[collectionView cellForItemAtIndexPath:indexPath];
    ZFBHomeBottomList *bottomList = cell.bottomList;
    
    if ([self.delegate respondsToSelector:@selector(homeBottomView:title:index:)]) {
        [self.delegate homeBottomView:self title:bottomList.name index:indexPath.item];
    }
}

@end
